<?php
/**
 * @author Alecsandru Gabriel DINU
 * @since Jun 13, 2008
 */
if (!class_exists('Logger')) {
	require_once 'src/main/classes/core/log/Log.php';
	
	
	/**
	 * Simple loggind facade factory class.
	 */
	class Logger implements Singleton_Interface {
		
	    private static $theInstance;
	    private $logFile;
	    
	    private function __construct() {
	        $this->load();
	    }

	    public static function getInstance() {
	        if (!self::$theInstance) {
	            self::$theInstance = new Logger();
	        }
	        
	        return self::$theInstance;
	    }

	    public function getLogger() {
	    	$trace = $this->getCallingClass();

	    	$class = "";
	    	if (isset($trace["class"])) {
	    		$class = $trace["class"];
	    	}
	    	
	    	$file = "";
	    	if (isset($trace["file"])) {
	    		$file = $trace["file"];
	    	}
	    	
	    	$log = new Log($file, $class, $this->logFile);
	    	return $log;
	    }

	    private function load() {
			$config = AlfaConfiguration::getInstance();
	        $logDir = $config->getString('logging/logDir');
	        $logFileName = $config->getString('logging/logFileName');
	        if ($logDir && $logFileName) {
	            $this->logFile = $logDir ."/" .$logFileName;
	            ini_set('error_log', $this->logFile);
	        }
	    }


	    private function getCallingClass() {
	    	$trace = debug_backtrace();
	    	return $trace[2];
	    }

		/**
	     * Override clone method to throw exception
	     * @access public
	     * @return void
	     * @throws Exception
	     */
	    public function __clone() {
	    	throw new Exception('You cannot clone singleton object');
	    }
	}
}